package com.commerce.reader.Activity;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.commerce.reader.Entity.Book;
import com.commerce.reader.Entity.Books;


import static com.commerce.reader.utils.JsoupHtml.toBookUrl;


public class DescViewModel extends ViewModel {
    // TODO: Implement the ViewModel

    private final static int UPDATE_MAP = 1;
    private MutableLiveData<Book> mBook;
    private Book book;
    private Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case UPDATE_MAP:
                    mBook.setValue(book);
                    break;
                default:
            }
            super.handleMessage(msg);
        }
    } ;

    public DescViewModel() {
        mBook = new MutableLiveData<>();
    }
    public LiveData<Book> getBook(final Books books) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                book = toBookUrl(books.getUrl());
                Message message = new Message();
                message.what = UPDATE_MAP;
                handler.sendMessage(message);
            }
        }).start();
        return mBook;
    }

}